#include "Player.h"

Player::Player()
{
	for(int i=0; i < 12; ++i)
	{
		deckCards[i] = NULL;
		i < 6 ? handCards[i] = NULL : 0;
		i < 7 ? fieldCards[i] = NULL : 0;
	}
	firstTurn = true;
}

Player::~Player()
{
	for(int i=0; i < 12; ++i)
	{
		delete deckCards[i];
		deckCards[i] = NULL;
		i < 6 ? handCards[i] = NULL : 0;
		i < 7 ? fieldCards[i] = NULL : 0;
	}
}

void Player::InitDeck(char* fileName, dxManager* dxMgr)
{
	ifstream infile;
	infile.open(fileName, ifstream::in);
	if(!infile.is_open())
		exit(1);
	cardDataParser readCard;
	readCard.ReadDataFromFile(infile);
	readCard.getData(deckCards);
	infile.close();

	for(int i=0; i < 12; ++i)
		deckCards[i]->SetCardImage(dxMgr);
}

void Player::SuffleDeck()
{
	CardData* tempPt = NULL;
	short swapNum1 = 0;
	short swapNum2 = 0;

	for(int i=0; i < 100; ++i)
	{
		swapNum1 = rand() % (12); //rand 0-11
		swapNum2 = rand() % (12);

		tempPt = deckCards[swapNum1];
		deckCards[swapNum1] = deckCards[swapNum2];
		deckCards[swapNum2] = tempPt;
	}
}

void Player::DrawCardsFromDeckToHand()
{
	if(firstTurn)
	{
		firstTurn = false;
		for(int i=0; i < 6; ++i) //take the first 6 cards from deck and place them to hand
			handCards[i] = deckCards[i];
	}
	else
	{
		for(int handNum=0,deckNum=6; handNum < 6; ++handNum) //take next six cards and place them on empty hand slots
			if(handCards[handNum] == NULL)
				handCards[handNum] = deckCards[deckNum++];
	}
}

CardData* Player::ShowLastCardInDeck()
{
	return deckCards[11];
}

void Player::DiscardHandCards(int number)
{
	handCards[number] = NULL;
}

void Player::SwapHandAndField(int handNum, int fieldNum)
{
	CardData* temp = handCards[handNum];
	handCards[handNum] = fieldCards[fieldNum];
	fieldCards[fieldNum] = temp;
}

bool Player::HasHandCard(int number)
{
	if(handCards[number] != NULL)
		return true;
	return false;
}

bool Player::HasFieldCard(int number)
{
	if(fieldCards[number] != NULL)
		return true;
	return false;
}

CardData* Player::ShowHandCard(int number)
{
	return handCards[number];
}

CardData* Player::ShowFieldCard(int number)
{
	return fieldCards[number];
}